package com.bdmc.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.base.domain.bo.BaseIconBo;
import com.bdmc.base.domain.vo.BaseIconVo;
import com.bdmc.base.domain.BaseIcon;
import com.bdmc.base.mapper.BaseIconMapper;
import com.bdmc.base.service.IBaseIconService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 资源图标Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Service
public class BaseIconServiceImpl extends ServicePlusImpl<BaseIconMapper, BaseIcon, BaseIconVo> implements IBaseIconService {

    @Override
    public BaseIconVo queryById(Long iconId){
        return getVoById(iconId);
    }

    @Override
    public TableDataInfo<BaseIconVo> queryPageList(BaseIconBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseIcon> lqw = buildQueryWrapper(bo);
        Page<BaseIconVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<BaseIconVo> queryList(BaseIconBo bo) {
        LambdaQueryWrapper<BaseIcon> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<BaseIcon> buildQueryWrapper(BaseIconBo bo) {
        LambdaQueryWrapper<BaseIcon> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDictName()), BaseIcon::getDictName, bo.getDictName());
        lqw.eq(StringUtils.isNotBlank(bo.getDictType()), BaseIcon::getDictType, bo.getDictType());
        lqw.eq(StringUtils.isNotBlank(bo.getDictLabel()), BaseIcon::getDictLabel, bo.getDictLabel());
        lqw.eq(bo.getDictValue() != null, BaseIcon::getDictValue, bo.getDictValue());
        lqw.eq(StringUtils.isNotBlank(bo.getOnlineIcon()), BaseIcon::getOnlineIcon, bo.getOnlineIcon());
        lqw.eq(StringUtils.isNotBlank(bo.getOfflineIcon()), BaseIcon::getOfflineIcon, bo.getOfflineIcon());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmIcon()), BaseIcon::getAlarmIcon, bo.getAlarmIcon());
        lqw.eq(StringUtils.isNotBlank(bo.getPrewarnIcon()), BaseIcon::getPrewarnIcon, bo.getPrewarnIcon());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BaseIconBo bo) {
        BaseIcon add = BeanUtil.toBean(bo, BaseIcon.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setIconId(add.getIconId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BaseIconBo bo) {
        BaseIcon update = BeanUtil.toBean(bo, BaseIcon.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BaseIcon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
